package com.rongzhe.house.common.global_exception_handle;


import com.rongzhe.house.common.constants.Constants;
import com.rongzhe.house.common.exception.RongZheBusinessException;
import com.rongzhe.house.common.resp.ApiResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

@Slf4j
@ControllerAdvice
public class GlobalExceptionHandler {

    @ExceptionHandler(RongZheBusinessException.class)
    @ResponseBody
    public ApiResult handleBizExp(HttpServletRequest request, Exception ex) {
        log.info("Business exception handler  " + ex.getMessage());

        ApiResult apiResult = new ApiResult();
        apiResult.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
        apiResult.setMessage("内部业务错误");

        return apiResult;
    }

    @ExceptionHandler(Exception.class)
    public ApiResult handSql(Exception ex) {
        log.info("Unknown Exception " + ex.getMessage());

        ApiResult apiResult = new ApiResult();
        apiResult.setCode(Constants.RESP_STATUS_INTERNAL_ERROR);
        apiResult.setMessage("内部错误");
        return apiResult;
    }
}
